package cn.rengy.tpa.wxmp.menu;



import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.rengy.tool.core.util.ExceptionPrintUtils;
import cn.rengy.tool.http.HttpUtils;
import cn.rengy.tpa.wxmp.exception.WeChatException;
import cn.rengy.tpa.wxmp.token.AccessToken;
import cn.rengy.tpa.wxmp.util.WeChatUtil;
/**
 * 微信菜单操作
 */
public class MenuManager {
	private static Logger logger = LoggerFactory.getLogger(MenuManager.class);
	
	private static final String MENU_CREATE_POST_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
	/**创建个性化菜单url**/
	private static final String MENU_CREATE_CONDITIONAL_URL = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=";
	
	/**删除个性化菜单url**/
	private static final String MENU_DEL_CONDITIONAL_URL = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=";
		
	private static final String MENU_GET_GET_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";
	private static final String MENU_DEL_GET_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
	private final int retry=2;
	private AccessToken accessToken;
	public MenuManager(AccessToken accessToken) {
		this.accessToken = accessToken;
	}
	/**
	 * 创建菜单
	 * @throws WeChatException 
	 */
	public void create(Menu menu) throws WeChatException{
		logger.info("创建菜单");
		for(int i=0;i<retry;i++){
			String resultStr = HttpUtils.post(MENU_CREATE_POST_URL+this.accessToken.token(), JSON.toJSONString(menu));
			int errCode=WeChatUtil.getErrCode(resultStr);
			if(errCode==40001){
				this.accessToken.refresh();
			}else{
				break;
			}
		}
	}
	/**
	 * 创建菜单
	 * @throws WeChatException 
	 */
	public void create(String menuJson){
		WeChatUtil.postSuccess(MENU_CREATE_POST_URL,menuJson,accessToken);
	}
	/**
	 * 创建个性化菜单
	 * @throws WeChatException 
	 * return 菜单id
	 */
	public String createConditional(String menuJson){
		logger.info("创建个性化菜单");
		String resultStr=null;
		for(int i=0;i<retry;i++){
			resultStr = HttpUtils.post(this.MENU_CREATE_CONDITIONAL_URL+this.accessToken.token(), menuJson);
			int errCode=WeChatUtil.getErrCode(resultStr);
			if(errCode==40001){
				this.accessToken.refresh();
			}else{
				break;
			}
		}
		try {
			Map map = JSONObject.parseObject(resultStr, Map.class);
			return (String)map.get("menuid");
		} catch (Exception e) {
			logger.error(ExceptionPrintUtils.getTrace(e));
		}
		return null;
	}
	/**
	 * 删除个性化菜单
	 * @throws WeChatException 
	 */
	public void delConditional(String menuId) {
		logger.info("删除个性化菜单");
		String menuJson="{\"menuid\":\""+menuId+"\"}";
		for(int i=0;i<retry;i++){
			String resultStr = HttpUtils.post(this.MENU_DEL_CONDITIONAL_URL+this.accessToken.token(), menuJson);
			int errCode=WeChatUtil.getErrCode(resultStr);
			if(errCode==40001){
				this.accessToken.refresh();
			}else{
				break;
			}
		}
	}
	/**
	 * 查询菜单
	 */
	public Menu getMenu() throws WeChatException {	
		logger.info("查询菜单");
		for(int i=0;i<retry;i++){
			String resultStr = HttpUtils.get(MENU_GET_GET_URL+this.accessToken.token());
			JSONObject jsonObject = JSONObject.parseObject(resultStr);
			Integer errCode =jsonObject.getIntValue("errcode");
			if(errCode==0){
				Menu menu = jsonObject.getObject("menu", Menu.class);
				return menu;
			}else if(errCode==40001){
				this.accessToken.refresh();
			}else{
				break;
			}
		}
		return null;
	}
	/**
	 * 删除菜单,包括个性化菜单
	 * @throws WeChatException 
	 */
	public void delete() throws WeChatException{
		logger.info("删除菜单");
		for(int i=0;i<retry;i++){
			String resultStr = HttpUtils.get(MENU_DEL_GET_URL+this.accessToken.token());
			int errCode=WeChatUtil.getErrCode(resultStr);
			if(errCode==40001){
				this.accessToken.refresh();
			}else{
				break;
			}
		}
	}
}
